package cn.wei.youvie.controller;

import cn.wei.youvie.common.exception.BusinessException;
import cn.wei.youvie.common.servlet.BaseResponse;
import cn.wei.youvie.common.servlet.DeleteRequest;
import cn.wei.youvie.common.servlet.ErrorCode;
import cn.wei.youvie.common.servlet.ResultUtils;
import cn.wei.youvie.domain.dto.content.ContentAddRequest;
import cn.wei.youvie.domain.dto.content.ContentUpdateRequest;
import cn.wei.youvie.domain.entity.Content;
import cn.wei.youvie.domain.vo.ContentVO;
import cn.wei.youvie.service.ContentService;
import cn.wei.youvie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/content")
@Slf4j
public class ContentController {

    @Resource
    private ContentService contentService;

    @Resource
    private UserService userService;

    /**
     * 新建章节
     */
    @PostMapping("/add")
    public BaseResponse<Integer> addContent(@RequestBody ContentAddRequest contentsAddRequest, HttpServletRequest request) {
        Integer contentId = contentService.add(contentsAddRequest, request);
        return ResultUtils.success(contentId);
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteContent(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        boolean removed = contentService.deleteContent(deleteRequest, request);
        return ResultUtils.success(removed);
    }

    /**
     * 更新（仅管理员）
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateContent(@RequestBody ContentUpdateRequest contentsUpdateRequest, HttpServletRequest request) {
        boolean updated = contentService.updateContent(contentsUpdateRequest, request);
        return ResultUtils.success(updated);
    }

    /**
     * 根据 id 获取
     */
    @GetMapping("/get/vo")
    public BaseResponse<ContentVO> getContentVOById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Content content = contentService.getById(id);
        if (content == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(contentService.getContentVO(content, request));
    }

}
